General Performance Tips
Since applications built with LiveControls are intended to replicate the responsiveness of Windows applications as much as possible, server/network performance is a top priority, as even a delay as small as a few hundred milliseconds could be noticeable from a user standpoint (depending on the application).
While every situation is different, the following is a list of elements to check and techniques to use when writing LiveControl applications which will increase performance.
Be sure network/server hardware meets requirements.
While this is an obvious first step when creating ANY web application, if your goal is to create an optimally responsive web-based UI, your hardware and network connection is even more important. Of course, the spectrum of possible network/server configurations is enormous, especially since this configuration is application and traffic specific. As such, it is beyond the scope of this document but something to keep in mind when planning your application.
Be sure IIS is tuned optimally for your application.
Again, server tuning is a pretty basic step for creating web applications. If you are noticing excessive lag time, however, you may want to refer to the following MSDN articles for suggestions:
http://support.microsoft.com/kb/308186
http://support.microsoft.com/kb/814876
Use string concatenation techniques.
String concatenation commonly does not have negative implications. For example, the following code...
for(int i=0; i<100; i++) Label1.Text+= "Item " + i + "<br>";
...would work fine when using standard controls. However, when using LiveControls, this would cause 100 lines of redundant JavaScript code be sent to the client. Instead, perform the concatenation like so:
string txt = ""; for(int i=0; i<100; i++) txt+= "Item " + i + "<br>"; LiveLabel1.Text = txt;
This would only result in one line of JavaScript being sent to the client.
Only update Microsoft/3rd party server controls when necessary.
By default, all Microsoft controls are ignored, and not updated within a callback. To have a Microsoft (or other 3rd party control) be automatically updated, add a "LiveControlUpdate='true'" attribute to the server control.
<asp:Label ID="Label1" runat="server" LiveControlUpdate="true">Hello World</asp:Label>
Use Session DataPersistenceType.
The DataPersistenceType property has been obsoleted. By default, LiveControls now uses a ViewState caching mechanism, which has a number of benefits. ViewState will no longer be round-tripped with each callback, greatly reducing bandwidth requirements and accelerating client-side rendering operations. The difference will be most noticeable in complex pages with many controls. For more information on this, see ViewState Management Techniques in Optimizing Performance. For users who wish to implement a custom ViewState solution, ViewState caching can be disabled in the web.config by adding the following key:
<add key="Dart.LiveControls.ViewstateCacheEnabled" value="false" />
See the Override Page.LoadPageStateFromPersistenceMedium and Page.SavePageStateToPersistenceMedium topic below for information on implementing a custom ViewState solution.
Override Page.LoadPageStateFromPersistenceMedium and Page.SavePageStateToPersistenceMedium.
In previous versions of LiveControls, page-level ViewState elements could be handled by overriding the Page.LoadPageStateFromPersistenceMedium and Page.SavePageStateToPersistenceMedium function calls. With ViewState caching this is no longer necessary. However, these functions can still be overridden for custom ViewState implementations. The developer is provided the opportunity to store (and retrieve) ViewState information to disk or a database, for example:
protected override object LoadPageStateFromPersistenceMedium() { //add code to return the saved ViewState object } protected override void SavePageStateToPersistenceMedium(object viewState) { //add code to store the saved ViewState object }
Use the diagnostic mode to see the data sent and received during the callback.
To use the diagnostic mode, set the Debug property of any LiveControl to true. This will enable you to see what data is sent with the callback, and the JavaScript that is executed when the callback completes. This is useful for identifying redundant JavaScript code.
ViewState Management Techniques
State management when using LiveControls may require special emphasis. Since the application has Windows-style response to interface interactions, and provides controls associated with Windows applications such as LiveTimer, it is quite easy to forget that the communication between client and server still uses the HTTP protocol, and is therefore stateless.
As with ASP.NET controls, the LiveControls require "ViewState" techniques to carry forth control state from one request to the next. By default, LiveControls now stores ViewState in the application's Cache object, which has a number of benefits. ViewState will no longer be round-tripped with each callback, greatly reducing bandwidth requirements and accelerating client-side rendering operations. The difference will be most noticeable in complex pages with many controls.
Each page's viewstate data is stored with a default timeout of 20 minutes. This value can be altered by adding the following key to the appSettings node in the application's web.config file:
<add key="Dart.LiveControls.ViewStateCacheTimeout" value="20" />
The value should be set to the number of minutes before expiration. It is recommended that this timeout be equal to or greater than the setting used for the application's Session timeout. If the ViewState cache expires while the session remains active, an exception will occur if the page is manipulated:
"The server viewstate cache has timed out. The application was unable to successfully recover state. To address this exception, modify your web.config to use client-side viewstate caching, or to extend the server cache timeout value."
One technique to prevent an expired cache is to use a LiveTimer control to periodically raise a callback. A LiveTimer with an interval slightly less that the cache Timeout will ensure the state of the page is always kept alive.
To disable the ViewState cache mechanism and to force the encoded data to be round-tripped in each callback, add the following key to the appSettings node in the application's web.config file:
<add key="Dart.LiveControls.ViewStateCacheEnabled" value="false" />
If you would like to implement a custom ViewState persistence mechanism, you may override Page.LoadPageStateFromPersistenceMedium and Page.SavePageStateToPersistenceMedium to store and retrieve ViewState information in a custom manner (for example, using a database or file system):
protected override object LoadPageStateFromPersistenceMedium() { //add code to return the saved ViewState object } protected override void SavePageStateToPersistenceMedium(object viewState) { //add code to store the saved ViewState object }